/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: serializer.h
 *
 * Purpose: serializer base class implementation
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __SERIALIZER_H__
#define __SERIALIZER_H__

#include <vector>
#include <string>
#include <array>
#include <map>
#include <stdint.h>
#include <memory>
#include <utility>

#include "internal_impl.h"
#include "serializable.h"

/******************************************************************************
 **    MACROS
 ******************************************************************************/
//the default size of serializer buffer
#define DEFAULT_BUF_SIZE (256)

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
class Serializer
{
public:
    using Payload = std::vector<uint8_t>;
    using PayloadPtr = std::shared_ptr<Payload>;
public:
    Serializer();
    virtual ~Serializer();
public:
    virtual PayloadPtr GetPayload() const { return payload_;};

public:
    virtual bool Serialize(bool val);
    virtual bool Serialize(int8_t val);
    virtual bool Serialize(int16_t val);
    virtual bool Serialize(int32_t val);
    virtual bool Serialize(int64_t val);
    virtual bool Serialize(uint8_t val);
    virtual bool Serialize(uint16_t val);
    virtual bool Serialize(uint32_t val);
    virtual bool Serialize(uint64_t val);
    virtual bool Serialize(float val);
    virtual bool Serialize(double val);
    virtual bool Serialize(const std::string& val);
    virtual bool Serialize(const Seriablizable& ser);
    virtual bool Serialize(const uint8_t* data, uint32_t length);
public:

    template<typename T>
    bool Serialize(const std::vector<T>& val)
    {/** treat vector as dynamic array, |val.size()| element values...| */

        if (!Serialize((uint32_t)val.size()))
        {
            return false;
        }

        for (auto it : val)
        {
            if (!Serialize(it))
            {
                return false;
            }
        }

        return true;
    }

    template<typename T, std::size_t N>
    bool Serialize(const std::array<T, N>& val)
    {
        if (!Serialize((uint32_t)val.size()))
        {
            return false;
        }

        for (auto it : val)
        {
            if (!Serialize(it))
            {
                return false;
            }
        }

        return true;
    }


    template<typename T, class=typename std::enable_if<std::is_enum<T>::value >::type >
    bool Serialize(T val) /** Serialize for enum */
    {
        return Serialize((typename internal_impl::enum2Int<sizeof(T)>::type)val);
    }

    template<typename Key, typename Value>
    bool Serialize(const std::map<Key, Value>& val)
    {
        if (!Serialize((uint32_t)val.size()))
        {
            return false;
        }

        for (auto it : val)
        {
            if (!Serialize(it->first) || !Serialize(it->second))
            {
                return false;
            }
        }

        return true;
    }

    template<typename T, typename ...Args>
    void Serialize(T& t, Args...)
    {
        if (!Serialize(std::forward<T>(t)))
        {
            return false;
        }

        return Serialize(Args...);
    }
protected:
    PayloadPtr payload_;


};

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
using SerializerPtr = std::shared_ptr<Serializer>;
#endif /** !__SERIALIZER_H__ */





